package com.woniuxy.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.util.ObjectCoverUtil;
import com.woniuxy.core.util.StateNum;
import com.woniuxy.goods.dto.CatPictureListDto;
import com.woniuxy.goods.dto.GoodsCatPictureDto;
import com.woniuxy.goods.dto.GroupTypeDto;
import com.woniuxy.goods.entity.GoodsCat;
import com.woniuxy.goods.entity.GoodsCatPicture;
import com.woniuxy.goods.mapper.GoodsCatPictureMapper;
import com.woniuxy.goods.param.AddCatPictureParam;
import com.woniuxy.goods.param.UpdateCatPictureByCatParam;
import com.woniuxy.goods.param.UpdateCatPictureParam;
import com.woniuxy.goods.service.GoodsCatPictureService;
import com.woniuxy.goods.service.GoodsCatService;
import com.woniuxy.goods.service.GroupTypeService;
import com.woniuxy.web.util.ConvertUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * 类别定义图服务层实现类
 * @author ...
 * @since 2024年05月27日
 */
@Service
public class GoodsCatPictureServiceImpl extends ServiceImpl<GoodsCatPictureMapper, GoodsCatPicture> implements GoodsCatPictureService {

    /**
     * 类别管理服务层接口
     */
    @Resource
    private GoodsCatService catService;
    /**
     * 类别属性组管理服务层接口
     */
    @Resource
    private GroupTypeService groupTypeService;

    /**
     * 新增类别定义图
     * @param param 新增定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public GoodsCatPicture addCatPicture(AddCatPictureParam param) throws Exception {
        GoodsCat cat = catService.findCatById(param.getCatId());
        if (cat==null) throw new Exception("所选类别不存在");
        QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
        wrapper.eq("picture_name",param.getPictureName());
        wrapper.eq("cat_id",param.getCatId());
        GoodsCatPicture catPicture = getOne(wrapper);
        if (catPicture!=null) throw new Exception("该类别已存在此定义图");
        GoodsCatPicture bean = BeanUtil.toBean(param, GoodsCatPicture.class);
        save(bean);
        return bean;
    }

    /**
     * 删除类别定义图
     * @param id 定义图id
     * @return
     * @throws Exception
     */
    @Override
    public void delCatPicture(Integer id) {
        removeById(id);
    }

    /**
     * 修改类别定义图
     * @param param 修改定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public GoodsCatPicture updateCatPicture(UpdateCatPictureParam param) throws Exception {
        GoodsCatPicture catPicture = getById(param.getId());
        if (catPicture==null) throw new Exception("该类别定义图不存在");
        if (!param.getPictureName().equals(catPicture.getPictureName())){
            QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
            wrapper.eq("picture_name",param.getPictureName());
            GoodsCatPicture one = getOne(wrapper);
            if (one!=null){
                throw new Exception("已存在该定义图名称");
            }
        }
        GoodsCatPicture bean = BeanUtil.toBean(param, GoodsCatPicture.class);
        updateById(bean);
        return bean;
    }

    /**
     * 根据类别id修改类别定义图
     * @param param 修改定义图对象
     * @return 定义图信息
     * @throws Exception
     */
    @Override
    public List<GoodsCatPicture> updateCatPictureByCat(UpdateCatPictureByCatParam param) throws Exception {
        QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",param.getCatId());
        remove(wrapper);
        List<GoodsCatPicture> pictures = new ArrayList<>();
        for (AddCatPictureParam paramParam : param.getParams()) {
            paramParam.setCatId(param.getCatId());
            GoodsCatPicture catPicture = addCatPicture(paramParam);
            pictures.add(catPicture);
        }
        return pictures;
    }

    /**
     * 根据id查询类别定义图
     * @param id 定义图id
     * @return 定义图信息
     * @throws Exception
     */
    @Override
    public GoodsCatPicture findCatPictureById(Integer id) {
        return getById(id);
    }

    /**
     * 根据类别id查询类别定义图
     * @param catId 类别id
     * @return 定义图信息集合
     * @throws Exception
     */
    @Override
    public CatPictureListDto findCatPictureByCatId(Integer catId) throws Exception {
        GoodsCat cat = catService.findCatById(catId);
        if (cat==null) throw new Exception("类别不存在");
        CatPictureListDto dto = new CatPictureListDto();
        List<GoodsCatPicture> must = new ArrayList<>();
        List<GoodsCatPicture> could = new ArrayList<>();
        //一级类别
        if (cat.getUpCatId()==0){
            QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
            wrapper.eq("cat_id",catId);
            wrapper.eq("is_choice",StateNum.PICTURE_COULD_CHOICE);
            could = list(wrapper);
            List<GoodsCatPictureDto> convertListTo = ConvertUtil.convertListTo(could, GoodsCatPictureDto::new);
            dto.setCouldChoice(convertListTo);
            QueryWrapper<GoodsCatPicture> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("cat_id",catId);
            wrapper1.eq("is_choice",StateNum.PICTURE_MUST_CHOICE);
            must = list(wrapper1);
            List<GoodsCatPictureDto> convert = ConvertUtil.convertListTo(must, GoodsCatPictureDto::new);
            dto.setMustChoice(convert);
            return dto;
        }else {
            //二级类别
            QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
            wrapper.eq("cat_id",catId);
            List<GoodsCatPicture> lower = list(wrapper);
            QueryWrapper<GoodsCatPicture> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("cat_id",cat.getUpCatId());
            List<GoodsCatPicture> up = list(wrapper1);
            List<GoodsCatPicture> find = new ArrayList<>(lower);
            for (GoodsCatPicture catPicture : up) {
                boolean bool = true;
                for (GoodsCatPicture picture : lower) {
                    if (picture.getPictureName().equals(catPicture.getPictureName())){
                        bool = false;
                        if (catPicture.getIsChoice()==StateNum.PICTURE_MUST_CHOICE){
                            find.remove(picture);
                            bool = true;
                        }
                    }
                }
                if (bool){
                    find.add(catPicture);
                }
            }
            for (GoodsCatPicture picture : find) {
                if (picture.getIsChoice()==StateNum.PICTURE_MUST_CHOICE){
                    must.add(picture);
                }else{
                    could.add(picture);
                }
            }
            List<GoodsCatPictureDto> convertMust = ConvertUtil.convertListTo(must, GoodsCatPictureDto::new);
            List<GoodsCatPictureDto> convertCould = ConvertUtil.convertListTo(could, GoodsCatPictureDto::new);
            dto.setMustChoice(convertMust);
            dto.setCouldChoice(convertCould);
            return dto;
        }
    }

    /**
     * 根据类别属性组id查询类别定义图
     * @param id 类别属性组id
     * @return 定义图信息集合
     * @throws Exception
     */
    @Override
    public CatPictureListDto findPictureById(Integer id) throws Exception {
        GroupTypeDto groupType = groupTypeService.findGroupTypeById(id);
        if (groupType==null) throw new Exception("类别属性组绑定信息不存在");
        CatPictureListDto pictureList = this.findCatPictureByCatId(groupType.getCatId());
        return pictureList;
    }

    /**
     * 根查询类别自己的定义图
     * @param catId 类别id
     * @return 定义图信息集合
     * @throws Exception
     */
    @Override
    public List<GoodsCatPicture> findPictureByCat(Integer catId) {
        QueryWrapper<GoodsCatPicture> wrapper = new QueryWrapper<>();
        wrapper.eq("cat_id",catId);
        List<GoodsCatPicture> pictures = list(wrapper);
        return pictures;
    }
}
